package com.xian.tszs.meal.business;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xian.tszs.meal.entity.TsAdmin;
import com.xian.tszs.meal.service.StatisticService;

/**
 * 统计层
 * 
 * @Title: StatisticManageController.java
 * @author MRC
 * @date 2019年5月28日 上午11:05:29
 * @version V1.0
 */
@Controller
@RequestMapping("business/category")
public class StatisticManageController {

	@Autowired
	private StatisticService statisticService;// 统计服务层

	/***
	 * 请求统计上周订单数
	 * 
	 * @author: MRC
	 * @date 2019年5月28日 上午11:10:54
	 * @return Map<String,Object> 返回类型
	 */
	@PostMapping("countLastWeekOrderNum")
	@ResponseBody
	public Map<String, Object> countLastWeekOrderNum(@AuthenticationPrincipal TsAdmin logined) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("success", true);
		map.put("data", statisticService.countLastWeekOrderNum(logined.getId()));

		return map;

	}

	/***
	 * 收集近七天收入情况
	 * 
	 * @author: MRC
	 * @date 2019年5月28日 上午11:50:17
	 * @return Map<String,Object> 返回类型
	 */
	@PostMapping("countLastWeekOrderAmount")
	@ResponseBody
	public Map<String, Object> countLastWeekOrderAmount(@AuthenticationPrincipal TsAdmin logined) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("success", true);
		map.put("data", statisticService.countLastWeekOrderAmount(logined.getId()));

		return map;

	}

	/***
	 * 统计商家得注册用户
	 * 
	 * @author: MRC
	 * @date 2019年5月28日 下午12:03:09
	 * @return Map<String,Object> 返回类型
	 */
	@PostMapping("countBusinessUser")
	@ResponseBody
	public Map<String, Object> countBusinessUser(@AuthenticationPrincipal TsAdmin logined) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("success", true);
		map.put("data", statisticService.countBusinessUser(logined.getId()));

		return map;

	}

	/***
	 * 获得服务器内存使用率
	 * 
	 * @author: MRC
	 * @date 2019年5月28日 下午1:38:01
	 * @return Map<String,Object> 返回类型
	 * @throws SigarException 
	 */
	@PostMapping("getMemoryUsed")
	@ResponseBody
	public Map<String, Object> getMemoryUsed() throws SigarException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		Sigar sigar = new Sigar();
		Mem mem = sigar.getMem();
		
		long total = mem.getTotal() / 1024L;
		long userd = mem.getUsed() / 1024L;
		double rate =userd * 1.0 /total *100 ;
		DecimalFormat df = new DecimalFormat("#");
		String str = df.format(rate);
		
		map.put("success", true);
		map.put("data", str);

		return map;

	}

}
